/**************************************************************************************************
	$Header: /pub/cvsroot/mydns/src/util/import.c,v 1.5 2002/09/12 04:50:42 bboy Exp $
	import.c: Import DNS data.

	Copyright (C) 2002  Don Moore <bboy@bboy.net>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at Your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**************************************************************************************************/

#include "util.h"


int opt_drop_zone = 0;											/* Drop all RR's before importing? */

extern void import_axfr(char *hostport, char *import_zone);
#ifdef TINYDNS_IMPORT_IMPORT
extern void import_tinydns(char *datafile, char *import_zone);
#endif

enum _input_format {												/* Import format types */
	INPUT_UNKNOWN,
	INPUT_AXFR,
#ifdef TINYDNS_IMPORT
	INPUT_TINYDNS
#endif
} input_format = INPUT_UNKNOWN;

char *axfr_host = NULL;											/* Host to import via AXFR */
#ifdef TINYDNS_IMPORT
char *tinydns_datafile = NULL;								/* Name of tinydns-data format file */
#endif


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	USAGE
	Display program usage information.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static void
usage(status)
	int status;
{
	if (status != EXIT_SUCCESS)
	{
		fprintf(stderr, _("Try `%s --help' for more information."), progname);
		fputs("\n", stderr);
	}
	else
	{
		printf(_("Usage: %s [OPTION]... [ZONE]..."), progname);
		puts("");
		puts(_("Import DNS data."));
		puts("");
/*		puts("----------------------------------------------------------------------------78");  */
		puts(_("  -a, --axfr=HOST         import zones from HOST via AXFR"));
#ifdef TINYDNS_IMPORT
		puts(_("  -t, --tinydns=FILE      import zones from tinydns-data format FILE"));
#endif
		puts("");
		puts(_("  -d, --drop-zone         after inserting SOA, delete all current RRs"));
		puts("");
		puts(_("  -v, --verbose           be more verbose while running"));
		puts(_("      --help              display this help and exit"));
		puts(_("      --version           output version information and exit"));
		puts("");
		puts(_("If no ZONE is specified, all zones found will be imported.  If AXFR"));
		puts(_("is being used as the import method, the zone names must be specified."));
		puts("");
		printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
	}
	exit(status);
}
/*--- usage() -----------------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	CMDLINE
	Process command line options.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
static void
cmdline(argc, argv)
	int argc;
	char *argv[];
{
	char	*optstr;
	int	optc, optindex;
	struct option const longopts[] =
	{
		{"axfr",			required_argument,	NULL,	'a'},
#ifdef TINYDNS_IMPORT
		{"tinydns",		required_argument,	NULL,	't'},
#endif

		{"drop-zone",	no_argument,			NULL,	'd'},
		{"verbose",		no_argument,			NULL,	'v'},

		{"help",			no_argument,			NULL,	0},
		{"version",		no_argument,			NULL,	0},

		{NULL, 0, NULL, 0}
	};

	err_stderr = err_plain = 1;
	error_init(argv[0], LOG_USER);							/* Init output routines */
	optstr = getoptstr(longopts);
	while ((optc = getopt_long(argc, argv, optstr, longopts, &optindex)) != -1)
	{
		switch (optc)
		{
			case 0:
				{
					const char *opt = longopts[optindex].name;

					if (!strcmp(opt, "version"))									/* --version */
					{
						printf("%s ("PACKAGE_NAME") "PACKAGE_VERSION"\n", progname);
						puts("\n" PACKAGE_COPYRIGHT);
						puts(_("This is free software; see the source for copying conditions.  There is NO"));
						puts(_("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."));
						exit(EXIT_SUCCESS);
					}
					else if (!strcmp(opt, "help"))								/* --help */
						usage(EXIT_SUCCESS);
				}
				break;
			case 'a':																	/* -a, --axfr */
				axfr_host = optarg;
				input_format = INPUT_AXFR;
				break;
#ifdef TINYDNS_IMPORT
			case 't':																	/* -t, --tinydns */
				tinydns_datafile = optarg;
				input_format = INPUT_TINYDNS;
				break;
#endif
			case 'd':																	/* -d, --drop-zone */
				opt_drop_zone = 1;
				break;
			case 'v':																	/* -v, --verbose */
				err_verbose = 1;
				break;
			default:
				usage(EXIT_FAILURE);
		}
	}
}
/*--- cmdline() ---------------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	MAIN
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
int
main(argc, argv)
	int argc;
	const char *argv[];
{
	setlocale(LC_ALL, "");										/* Internationalization */
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	load_config();
	cmdline(argc, argv);

	if (input_format == INPUT_UNKNOWN)						/* No input format specified */
	{
		Warnx(_("no input format specified"));
		usage(EXIT_FAILURE);
	}

	if (optind >= argc)											/* No arguments - try to import all zones */
	{
		switch (input_format)
		{
			case INPUT_AXFR:
				Warnx(_("using AXFR method; no zones specified"));
				usage(EXIT_FAILURE);
				break;

#ifdef TINYDNS_IMPORT
			case INPUT_TINYDNS:
				import_tinydns(tinydns_datafile, NULL);
				break;
#endif

			case INPUT_UNKNOWN:
				break;
		}
	}

	while (optind < argc)
	{
		switch (input_format)
		{
			case INPUT_AXFR:
				import_axfr(axfr_host, (char *)argv[optind]);
				break;

#ifdef TINYDNS_IMPORT
			case INPUT_TINYDNS:
				import_tinydns(tinydns_datafile, (char *)argv[optind]);
				break;
#endif

			case INPUT_UNKNOWN:
				break;
		}
		optind++;
	}

	return (0);
}
/*--- main() ------------------------------------------------------------------------------------*/

/* vi:set ts=3: */
/* NEED_PO */
